<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title> Terrarien Steuerung </title>
  <meta name="description" content="">
  <meta name="keywords" content="">
  <link rel="stylesheet" href="style.css" type="text/css">
</head>

<body>

<h3>Zeiteinstellung gespeichert!</h3>
<?php




// Get On- Off-times 
$ON_timex1 = $_POST["ON_time1"];
$ON_timex2 = $_POST["ON_time2"];
$ON_timex3 = $_POST["ON_time3"];
$ON_timex4 = $_POST["ON_time4"];

$OFF_timex1 = $_POST["OFF_time1"];
$OFF_timex2 = $_POST["OFF_time2"];
$OFF_timex3 = $_POST["OFF_time3"];
$OFF_timex4 = $_POST["OFF_time4"];




//Print on off times to file (Global Variables)
$datei = fopen(".save_cron_var","w");

$write_string="ON_time1=$ON_timex1\n";
fwrite($datei, $write_string);

$write_string="ON_time2=$ON_timex2\n";
fwrite($datei, $write_string);

$write_string="ON_time3=$ON_timex3\n";
fwrite($datei, $write_string);

$write_string="ON_time4=$ON_timex4\n";
fwrite($datei, $write_string);


$write_string="OFF_time1=$OFF_timex1\n";
fwrite($datei, $write_string);

$write_string="OFF_time2=$OFF_timex2\n";
fwrite($datei, $write_string);

$write_string="OFF_time3=$OFF_timex3\n";
fwrite($datei, $write_string);

$write_string="OFF_time4=$OFF_timex4\n";
fwrite($datei, $write_string);

fclose($datei);


//Convert to cron string
//1
$new_on1_1 = substr($ON_timex1, 0, 2);
$new_on1_2 = substr($ON_timex1, 3, 2);
$new_on1_3 ="$new_on1_2 $new_on1_1";

$new_off1_1 = substr($OFF_timex1, 0, 2);
$new_off1_2 = substr($OFF_timex1, 3, 2);
$new_off1_3 ="$new_off1_2 $new_off1_1";

//2
$new_on2_1 = substr($ON_timex2, 0, 2);
$new_on2_2 = substr($ON_timex2, 3, 2);
$new_on2_3 ="$new_on2_2 $new_on2_1";

$new_off2_1 = substr($OFF_timex2, 0, 2);
$new_off2_2 = substr($OFF_timex2, 3, 2);
$new_off2_3 ="$new_off2_2 $new_off2_1";

//3
$new_on3_1 = substr($ON_timex3, 0, 2);
$new_on3_2 = substr($ON_timex3, 3, 2);
$new_on3_3 ="$new_on3_2 $new_on3_1";

$new_off3_1 = substr($OFF_timex3, 0, 2);
$new_off3_2 = substr($OFF_timex3, 3, 2);
$new_off3_3 ="$new_off3_2 $new_off3_1";

//4
$new_on4_1 = substr($ON_timex4, 0, 2);
$new_on4_2 = substr($ON_timex4, 3, 2);
$new_on4_3 ="$new_on4_2 $new_on4_1";

$new_off4_1 = substr($OFF_timex4, 0, 2);
$new_off4_2 = substr($OFF_timex4, 3, 2);
$new_off4_3 ="$new_off4_2 $new_off4_1";

//system('chmod -R 0777 /var/spool/cron/',$result);

//First delete crontab
$datei = fopen("/var/www/crontab","w");

fclose($datei);

//Now write all Variables to crontab
$datei = fopen("/var/www/crontab","a");

$write_string="#-----------------------------------------------------------------\n\n";
fwrite($datei, $write_string);
$write_string="HOME=/var/www/scripts/terra_control\n";
fwrite($datei, $write_string);


//ON-OFF 1
if($new_on1_3 != " " || $new_off1_3 != " ")
{
	$write_string = "$new_on1_3 * * * /var/www/scripts/terra_control/control-system -w \n";
	fwrite($datei, $write_string);

	$write_string = "$new_off1_3 * * * /var/www/scripts/terra_control/control-system -s \n";
	fwrite($datei, $write_string);
}

//ON-OFF 2
if($new_on2_3 != " " || $new_off2_3 != " ")
{
	$write_string = "$new_on2_3 * * * /var/www/scripts/terra_control/control-system -w \n";
	fwrite($datei, $write_string);

	$write_string = "$new_off2_3 * * * /var/www/scripts/terra_control/control-system -s \n";
	fwrite($datei, $write_string);
}


//ON-OFF 3
if($new_on3_3 != " " || $new_off3_3 != " ")
{
	$write_string = "$new_on3_3 * * * /var/www/scripts/terra_control/control-system -w \n";
	fwrite($datei, $write_string);

	$write_string = "$new_off3_3 * * * /var/www/scripts/terra_control/control-system -s \n";
	fwrite($datei, $write_string);
}


//ON-OFF 4
if($new_on4_3 != " " || $new_off4_3 != " ")
{
	$write_string = "$new_on4_3 * * * /var/www/scripts/terra_control/control-system -w \n";
	fwrite($datei, $write_string);

	$write_string = "$new_off4_3 * * * /var/www/scripts/terra_control/control-system -s \n";
	fwrite($datei, $write_string);
}

$write_string="\n#-----------------------------------------------------------------\n\n";
fwrite($datei, $write_string);
fclose($datei);

//system('crontab /var/www/crontab', $result);

echo "---$new_on1_3---\n";
echo "<br>";
echo "---$new_on2_3---\n";
echo "<br>";
echo "---$new_on3_3---\n";
echo "<br>";
echo "---$new_on4_3---\n";
echo "<br>";

echo "---$new_off1_3---\n";
echo "<br>";
echo "---$new_off2_3---\n";
echo "<br>";
echo "---$new_off3_3---\n";
echo "<br>";
echo "---$new_off4_3---\n";
echo "<br>";


echo "$ON_timex1\n";
echo "<br>";
echo "$ON_timex2\n";
echo "<br>";
echo "$ON_timex3\n";
echo "<br>";
echo "$ON_timex4\n";

echo "$OFF_timex1\n";
echo "<br>";
echo "$OFF_timex2\n";
echo "<br>";
echo "$OFF_timex3\n";
echo "<br>";
echo "$OFF_timex4\n";
echo "<br>";







echo "</body>\n";
echo "</html>\n";
echo "\n";
?>


</body>
</html>
